package com.lwt.codetop.arrayOrSort;

// 704. 二分查找

/**
 * 思路：二分查找的模板题
 */
public class BinarySearch {
    public static void main(String[] args) {
        int[] nums = {-1,0,3,5,9,12};
        int target = 9;
        int res = search(nums, target);
        System.out.println(res);
    }

    private static int search(int[] nums, int target) {
        int n = nums.length;
        int l = 0, r = n - 1;
        while(l < r) {
            int mid = l + r >> 1;
            if(nums[mid] >= target) r = mid;
            else l = mid + 1;
        }
        if (nums[l] == target) return l;
        return -1;
    }
}
